package com.itheima.reggie.controller;


import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.R;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true) //解释见下面delete方法
    public R insert(@RequestBody SetmealDto setmealDto, HttpServletRequest request) {
        //生成createTime
        //生成updateTime
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(LocalDateTime.now());
        //生成createUser
        //生成updateUser
        Long isLogin = (Long) request.getSession().getAttribute("isLogin");
        setmealDto.setUpdateUser(isLogin);
        setmealDto.setCreateUser(isLogin);
        setmealService.insert(setmealDto);

        System.err.println("===" + setmealDto.getId());

        for (SetmealDish setmealDish : setmealDto.getSetmealDishes()) {
            setmealDish.setSetmealId(setmealDto.getId());//设置setmeal_id
            setmealDish.setCreateTime(LocalDateTime.now());//生成createTime
            setmealDish.setUpdateTime(LocalDateTime.now());//生成updateTime
            setmealDish.setCreateUser(isLogin);//生成createUser
            setmealDish.setUpdateUser(isLogin);//生成updateUser
            setmealDishService.insert(setmealDish);
        }

        return R.success("插入成功!");
    }

    @GetMapping("/page")
    public R selectBypage(int page, int pageSize, String name) {
        PageInfo<SetmealDto> pageInfo = setmealService.selectByPage(page, pageSize, name);
        return R.success(pageInfo);//已将前端取数据时改为res.data.list
    }

    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)//删除setmealCache分组下的全部key  如果只删除某个id的缓存,会漏删多条件组合查询的缓存,导致数据库缓存不一致
    public R delete(@RequestParam List<String> ids) {
        int count = 0;
        for (String id : ids) {
            String result = setmealService.delete(id);
            if (result.equals("success")) {
                count++;
            }
        }
        return R.success("成功删除了 " + count + " 个套餐,另外有 " + (ids.size() - count) + " 个套餐因为处于启售状态无法删除!");
    }

    @PostMapping("/status/0")//0表示停售
    public R updateStatus_0(@RequestParam List<String> ids) {
        for (String id : ids) {
            setmealService.updateStatus(id, 0);
        }
        return R.success("停售成功!");
    }

    @PostMapping("/status/1")//1表示启售
    public R updateStatus_1(@RequestParam List<String> ids) {
        for (String id : ids) {
            setmealService.updateStatus(id, 1);
        }
        return R.success("启售成功!");
    }

    @GetMapping("/{id}")
    public R selectAllById(@PathVariable String id) {
        SetmealDto setmealDto = setmealService.selectAllById(id);
        return R.success(setmealDto);
    }


    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R updateById(@RequestBody SetmealDto setmealDto, HttpServletRequest request) {
        //生成updateTime
        setmealDto.setUpdateTime(LocalDateTime.now());
        //生成updateUser
        Long isLogin = (Long) request.getSession().getAttribute("isLogin");
        setmealDto.setUpdateUser(isLogin);

        //修改 setmeal 表中 name,price,category_id,image,description,update_time,update_user属性
        setmealService.updateById(setmealDto);

        //先根据 setmeal_id 删除setmeal_dish表中的对应数据,再根据 setmealDto.getSetmealDishes()插入新的数据
        //根据 setmeal_id 删除对应行数据
        setmealDishService.deleteBySetmealId(setmealDto.getId());
        for (SetmealDish setmealDish : setmealDto.getSetmealDishes()) {
            //根据setmealDto.getSetmealDishes()插入新的数据
            //生成createTime updateTime createUser updateUser
            setmealDish.setCreateTime(LocalDateTime.now());//生成createTime
            setmealDish.setUpdateTime(LocalDateTime.now());//生成updateTime
            setmealDish.setCreateUser(isLogin);//生成createUser
            setmealDish.setUpdateUser(isLogin);//生成updateUser
            setmealDish.setSetmealId(setmealDto.getId());//设置setmeal_id
            setmealDishService.insert(setmealDish);
        }

        return R.success("修改成功");
    }

    @Cacheable(value = "setmealCache",key = "#categoryId")
    @GetMapping("/list")
    public R selectByCategoryId(String categoryId){
        List<Setmeal> list = setmealService.selectByCategoryId(categoryId);
        return R.success(list);
    }

}
